$(document).ready(function(){
	changeImg();
	$('#page_tpl').change(function() {changeImg();} );

	$('.fields .field .hendler').live('click', function(){
		$('.fields .field').removeClass('selected');
		$(this).parent('.field').addClass('selected');
	});

	$('.modules .module').click(function(){
		$('.modules .module').removeClass('selected');
		$(this).addClass('selected');
	});

	$('.addField').click(function(){
		var fields_lenght = $('.fields .field').length;
		var new_field = '<div rel="'+ fields_lenght +'" class="field"><div class="hendler"><strong>'+ fields_lenght +'</strong> - '+ (fields_lenght == 0 ? "Поле главного модуля" : "Поле") +'</div></div>';
		$('.fields').append(new_field);
	});

	$('.addModule').click(function(){
		var module = $('.modules .module.selected');
		var field = $('.fields .field.selected');

		if(module.length !== 1 || field.length !== 1) return;

		module_title = $(module[0]).html();
		module_id = $(module[0]).attr('rel');
		field_id = $(field[0]).attr('rel');

		if(field_id == 0 && $('.fields .field[rel="0"] .block').length > 0) return;

		newBlock(module_id, field_id);
	});

	$('.button.edit').live('click', function(){
		blockId = $(this).parent('.block').attr('rel');
		var url = '?module=pages&action=editblock&id='+ blockId;
		$.colorbox({
			href: url
		});
	});

	$('.button.up').live('click', function(){
		var blockId = $(this).parent('.block').attr('rel');
		var block = $('.block[rel="'+ blockId +'"]')
		var img = $(this);
		var src = img.attr('src');
		var prev = block.prev('.block');

		if(!block.length || !prev.length) return ;

		$.ajax({
			'url': '?module=pages&action=upblock&id='+ blockId,
			'dataType': 'json',
			'method': 'post',
			'beforeSend': function(){
				img.attr('src', '/tpl/admin/images/preloader.gif');
			},
			'success': function(data){
				img.attr('src', src);
				if(data > 0) prev.before(block);
			}
		});
	});

	$('.button.down').live('click', function(){
		var blockId = $(this).parent('.block').attr('rel');
		var block = $('.block[rel="'+ blockId +'"]')
		var img = $(this);
		var src = img.attr('src');
		var next = block.next('.block');

		if(!block.length || !next.length) return ;

		$.ajax({
			'url': '?module=pages&action=downblock&id='+ blockId,
			'dataType': 'json',
			'method': 'post',
			'beforeSend': function(){
				img.attr('src', '/tpl/admin/images/preloader.gif');
			},
			'success': function(data){
				img.attr('src', src);
				if(data > 0) next.after(block);
			}
		});
	});

	$('.button.delete').live('click', function(){
		blockId = $(this).parent('.block').attr('rel');
		var img = $(this);
		var src = img.attr('src');

		if(!confirm('Вы действительно хотите удалить этот блок?')) return;

		$.ajax({
			'url': '?module=pages&action=deleteblock&id='+ blockId,
			'dataType': 'json',
			'method': 'post',
			'beforeSend': function(){
				img.attr('src', '/tpl/admin/images/preloader.gif');
			},
			'success': function(data){
				img.attr('src', src);
				$('.block[rel="'+ blockId +'"]').remove();
			}
		});
	});

	$('#block_properties').live('submit', function(){
		var button = $(this).children('.save');
		$.ajax({
			'url': $(this).attr('action'),
			'data': $(this).serialize(),
			'dataType': 'json',
			'method': 'post',
			'beforeSend': function(){
				button.addClass('load');
			},
			'success': function(data){
				button.removeClass('load');
				
				if($('#block_id').val()){
					$('.block[rel="'+ $('#block_id').val() +'"]').replaceWith(data.block);
				} else {
					$('.field[rel="'+ data.field +'"]').append(data.block);
				}
				$.colorbox.close();
			}
		});
		return false;
	});

	$('#block_action').live('change', function(){
		$('#actionProperties').html("");
		$('#actionProperties').load('?module=pages&action=actionproperties', $('#block_properties').serialize(), function(){
			$.colorbox.resize();
		});
	});

	$('.copyBlocks').click(function(){
		var url = '?module=pages&action=copy_blocks&dest_page_id='+ $('#page_id').val();
		$.colorbox({
			href: url
		});
	});

	$('#src_page').live('change', function(){
		srcId = $('#src_page option:selected').val();
		$.ajax({
			'url': '?module=pages&action=load_src_blocks&src_id='+ srcId,
			'method': 'post',
			'dataType': 'html',
			'success': function(html){
				$('#src_blocks').html(html);
				$.colorbox.resize();
			}
		});
	});

	$('#copied_blocks_form').live('submit', function(){
		form = $(this);
		$.ajax({
			'url': form.attr('action'),
			'method': form.attr('mehtod'),
			'dataType': 'html',
			'data': form.serialize(),
			'success': function(html){
				alert(html);
				document.location.reload();
			}
		});
	});
})

function newBlock(module_id, field_id){
    var url = '?module=pages&action=createblock&module_id='+ module_id +'&field_id='+ field_id +'&page_id='+ $('#page_id').val();
    $.colorbox({
        href: url
    });
}

function changeImg(){
	var name = $('#page_tpl option:selected').html();
	$('#tpl_image').attr('src', '/tpl/'+ name +'/tpl.png');
}